﻿using AutoMapper;
using EquipmentModule.Contracts;
using EquipmentModule.Entity;
using EquipmentModule.ViewModel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;

namespace EquipmentModule.Controllers
{
    /// <summary>
    /// 设备
    /// </summary>
    [ApiController]
    [Route("Equipment/[controller]")]
    public class EquipmentController : ControllerBase
    {
        private readonly ILogger _logger;

        private readonly IWebHostEnvironment _env;
        private readonly IEquipmentService _service;
        private readonly IMapper _mapper;
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="env"></param>
        /// <param name="service"></param>
        public EquipmentController(IMapper mapper,ILogger<EquipmentController> logger, IWebHostEnvironment env, IEquipmentService service)
        {
            _logger = logger;
            _env = env;
            _service = service;
            _mapper = mapper;
        }
        /// <summary>
        /// 查询设备
        /// </summary>
        /// <param name="id">设备Id获取区域Id</param>
        /// <param name="sassId">账套Id</param>
        /// <param name="user">设备责任人</param>
        /// <param name="name">设备名称</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(Guid? id,Guid? sassId,string user,string name)
        {
            string db = Common.GetHeader4DB(this.Request);
            return Ok(new ResponseData(null,_service.Index(id,sassId,user,name,db)));
        }
        /// <summary>
        /// 根据Id删除设备
        /// </summary>
        /// <param name="id">设备Id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.Del(id,db);
            return Ok(new ResponseData(null,null));
        }
        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="info">设备</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(EquipmentViewModel4Add info)
        {
            string db = Common.GetHeader4DB(this.Request);
            if (info.Position==null || info.Position.Length!=2)
            {
                info.Position = new double?[] {null,null };
            }
            var newInfo= _mapper.Map<EquipmentViewModel4Add, Equipment>(info);
            newInfo.CreateTime = DateTime.Now;
            newInfo.Id = new Guid();
            _service.Add(newInfo,db);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 设备下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet("Downlist")]
        public IActionResult Index4Select()
        {
            string db = Common.GetHeader4DB(this.Request);
            return Ok(new ResponseData(null,_service.DownList(db)));
        }
        /// <summary>
        /// 分享设备给多个Sass账号
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost("Share")]
        public IActionResult ShareEquipment(EquipmentSassInfo info)
        {
            //string db = Common.GetHeader4DB(this.Request);
            _service.ShareEquipment(info);
            return Ok(new ResponseData(null, null));
        }

        /// <summary>
        /// 更新设备
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut("Update")]
        public IActionResult UpdateEquipment(EquipmentViewModel4Edit info)
        {
            if (info.Position==null || info.Position.Length!=2)
            {
                info.Position = new double?[] {null,null };
            }
            var newInfo= _mapper.Map<EquipmentViewModel4Edit, Equipment>(info);
            _service.Update(newInfo,null);
            return Ok(new ResponseData(null,null));
        }


        // [HttpGet("QueryBy")]
        // public IActionResult QueryBy(String ProjectId)
        // {
        //     Console.WriteLine(ProjectId);
        //     return Ok(new ResponseData(null,_service.QueryByProjectId(ProjectId)));
        // }
        
        
        /// <summary>
        /// 根据设备类型获取设备
        /// </summary>
        /// <param name="MachineType"></param>
        /// <returns></returns>
        [HttpGet("QueryByMachineType")]
        public IActionResult QueryByMachineType(String MachineType)
        {
            Console.WriteLine(MachineType);
            if (MachineType == null)
            {
                return Ok(new ResponseData(null,null));
            }
            return Ok(new ResponseData(null,_service.QueryByMachineType(MachineType)));
        }
    }
}
